package jehc.djshi.log.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.NeedLoginUnAuth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BasePage;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseSearch;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.common.util.date.DateUtils;
import jehc.djshi.log.model.XtLoadinfo;
import jehc.djshi.log.service.XtLoadinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;

/**
 * @Desc 页面加载信息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtLoading")
@Api(value = "页面加载信息API",tags = "页面加载信息API",description = "页面加载信息API")
public class XtLoadinfoController extends BaseAction {
	@Autowired
	private XtLoadinfoService xtLoadinfoService;
	/**
	* 加载初始化列表数据并分页
	* @param baseSearch
	*/
	@NeedLoginUnAuth
	@PostMapping(value="/list")
	@ApiOperation(value="查询页面加载信息列表并分页", notes="查询页面加载信息列表并分页")
	public BasePage getXtLoadinfoListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<XtLoadinfo> xt_LoadinfoList = xtLoadinfoService.getXtLoadinfoListByCondition(condition);
		PageInfo<XtLoadinfo> page = new PageInfo<XtLoadinfo>(xt_LoadinfoList);
		return outPageStr(page,baseSearch);
	}
	/**
	* 查询单个加载信息
	* @param xt_loadinfo_id
	*/
	@NeedLoginUnAuth
	@GetMapping(value="/get/{xt_loadinfo_id}")
	@ApiOperation(value="查询单个加载信息", notes="查询单个加载信息")
	public BaseResult getXtLoadinfoById(@PathVariable("xt_loadinfo_id")String xt_loadinfo_id){
		XtLoadinfo xt_Loadinfo = xtLoadinfoService.getXtLoadinfoById(xt_loadinfo_id);
		return outDataStr(xt_Loadinfo);
	}
	/**
	* 添加
	* @param xtLoadinfo
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个加载信息", notes="创建单个加载信息")
	public BaseResult addXtLoadinfo(@RequestBody XtLoadinfo xtLoadinfo){
		int i = 0;
		if(null != xtLoadinfo){
			xtLoadinfo.setXt_loadinfo_time((int)(Long.parseLong(xtLoadinfo.getXt_loadinfo_endtime())-Long.parseLong(xtLoadinfo.getXt_loadinfo_begtime())));
			xtLoadinfo.setXt_loadinfo_begtime(DateUtils.convert(Long.parseLong(xtLoadinfo.getXt_loadinfo_begtime())));
			xtLoadinfo.setXt_loadinfo_endtime(DateUtils.convert(Long.parseLong(xtLoadinfo.getXt_loadinfo_endtime())));
			xtLoadinfo.setXt_loadinfo_id(toUUID());
			xtLoadinfo.setXt_userinfo_id(getXtUid());
			i=xtLoadinfoService.addXtLoadinfo(xtLoadinfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	/**
	* 修改
	* @param xtLoadinfo
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个加载信息", notes="编辑单个加载信息")
	public BaseResult updateXtLoadinfo(@RequestBody XtLoadinfo xtLoadinfo){
		int i = 0;
		if(null != xtLoadinfo){
			i=xtLoadinfoService.updateXtLoadinfo(xtLoadinfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	/**
	* 删除
	* @param xt_loadinfo_id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除加载信息", notes="删除加载信息")
	public BaseResult delXtLoadinfo(String xt_loadinfo_id){
		int i = 0;
		if(!StringUtil.isEmpty(xt_loadinfo_id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("xt_loadinfo_id",xt_loadinfo_id.split(","));
			i=xtLoadinfoService.delXtLoadinfo(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	
	/**
	 * 分组统计
	 * @return
	 */
	@NeedLoginUnAuth
	@GetMapping(value="/group/list")
	@ApiOperation(value="分组统计", notes="分组统计")
	public BaseResult getXtLoadingGroupList(){
		JSONArray jsonArray = new JSONArray();  
		Map<String, Object> model = new HashMap<String, Object>();
		List<XtLoadinfo> xt_LoadinfoList = xtLoadinfoService.getXtLoadingGroupList();
		for(int i = 0; i < xt_LoadinfoList.size(); i++){
			XtLoadinfo xtLoadinfo = xt_LoadinfoList.get(i);
			model.put("menuTitle", xtLoadinfo.getXt_loadinfo_modules());
			model.put("loadingTime", xtLoadinfo.getXt_loadinfo_time());
			jsonArray.add(model);
		}
		return outItemsStr(jsonArray);
	}
}
